/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.actions;

import com.cardarus.operatingsystem.domain.Item;
import com.cardarus.operatingsystem.model.BusinessModel;
import com.cardarus.operatingsystem.struts.forms.StockForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * The action to add stock
 * @author bryan.hyde
 */
public class UpdateStockAction extends org.apache.struts.action.Action {

    /**
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     *
     * The class has a single function
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionErrors errors = new ActionErrors();
        StockForm sform = (StockForm) form;
        BusinessModel model = BusinessModel.getInstance();
        boolean isCard, isSpecial;
        isCard = isSpecial = false;
        if (sform.getItemTypes().equalsIgnoreCase("card")) {
            isCard = true;
        } else if (sform.getItemTypes().equalsIgnoreCase("specialbox")) {
            isSpecial = true;
        } else {
            isCard = false;
            isSpecial = false;
        }
        Item it = new Item();
        it.setId(sform.getStockID());
        it.setName(sform.getStockName());
        it.setDescription(sform.getStockDescription());
        it.setPrice(sform.getStockPrice());
        it.setInventory(sform.getStockInventory());
        it.setCard(isCard);
        it.setSpecialBox(isSpecial);

        model.updateItem(it);

        return mapping.findForward("success");
    }
}
